/**
 * Created by dengtao on 2016/3/7.
 */
$(function () {
    var _obj = {
        badge: false,
        newObj: function () {
            if (!this.badge) {
                this.init.prototype = _obj;
                this.badge = true;
            }

            this.init();

        },

        setArray: function () {//添加块
            var _disc = document.getElementById('disc');
            for (var i = 0; i < this.kcount; i++) {
                var el = document.createElement('div');
                el.setAttribute('class', 'lei');
                _disc.appendChild(el);
            }
        },
        setBomb: function () {//set地雷
            var i = 0;
            do {
                var m = Math.floor(Math.random() * this.kcount);//随机数
                m = (m.toString().length == 1) ? ('0' + m) : m.toString();//将个位变双位，方便清除重复
                if (this.leiOpList.join().indexOf(m) >= 0) {//清除重复
                    console.log('布雷时' + i + '次，' + m + '重复了');
                } else {
                    this.leiOpList.push(m);
                    i++;
                }
            } while (i < this.leiCount);//随机生成地雷

            for (var l = 0; l < this.leiCount; l++) {//将地雷布置在盘中
                //console.log(this._leiList[Number(this.leiOpList[l])]);
                this._leiList[Number(this.leiOpList[l])].setAttribute('class', 'lei ' + this.leiClassName);
            }
        },
        setLeiCount: function () {//set周围雷count
            var roundAry = [];
            for (var i = 0; i < this.kcount; i++) {
                //this.countRound(this._leiList[i]);
                roundAry = [];

                roundAry.push(this.countRound(this._leiList[i - this._x_count]));//8/上一个

                if (i % this._x_count != 0) {//如果是每排第一个 则不算左，上左，下左
                    roundAry.push(this.countRound(this._leiList[i - 1]));//8/左(前)一个
                    roundAry.push(this.countRound(this._leiList[i - this._x_count - 1]));//8/上左个
                    roundAry.push(this.countRound(this._leiList[i + this._x_count - 1]));//8/下左个
                }
                if (i % this._x_count != 9) {//如果是每排最后一个 则不算右，上右，下右
                    roundAry.push(this.countRound(this._leiList[i + 1]));//8/右(后)一个
                    roundAry.push(this.countRound(this._leiList[i - this._x_count + 1]));//8/上右个
                    roundAry.push(this.countRound(this._leiList[i + this._x_count + 1]));//8/下右个
                }
                roundAry.push(this.countRound(this._leiList[i + this._x_count]));//8/下一个

                this._leiList[i].setAttribute('roundCount', roundAry.join(',').split(this.leiClassName).length - 1);//set周围雷数量
                this._leiList[i].setAttribute('this_index', i);//set计算时下标
            }
        },
        countRound: function (obj) {//判断周围元素过当
            return obj ? obj.getAttribute('class') : '';
        },
        consoleLog: function (log) {//日志输出
            document.getElementById('loglab').value = log;
        },
        leiClick: function () {
            //_obj.consoleLog('左键点击了');
            if (this.getAttribute('class').indexOf(_obj.leiClassName) > 0 || !_obj.gameStatic) {
                _obj.gameStatic = false;
                _obj.consoleLog('GAMEOVER');
            } else {
                this.innerHTML = this.getAttribute('roundcount');
            }
        },
        leiRclick: function (e) {
            _obj.consoleLog('右键点击，阻止menu');
            console.log(e.targetUrl);
            return false;
        },
        bindEvent: function () {//事件
            var lei = document.getElementsByClassName('lei');//遍历绑定
            for (var i = 0; i < lei.length; i++) {
                lei[i].onclick = this.leiClick;//绑定左击
                lei[i].oncontextmenu = this.leiRclick;//绑定右击
            }

            this.startBtn.onclick = function () {
                _obj.gameStatic = true;
                _obj.consoleLog('开始');
            }
        },

        config: function () {
            this.leiClassName = 'isLei';
            this.kcount = 100;//块数量
            this.leiRate = 0.1;//雷比例
            this.leiCount = this.kcount * this.leiRate;//要布置雷数量
            this.leiOpList = [];//雷坐标list

            this._disc = document.getElementById('disc');//_盘
            this._leiList = document.getElementsByClassName('lei');//_块list
            this._lei_width = 30;//_雷宽度
            this._x_count = this._disc.offsetWidth / this._lei_width;//一排雷数量
            this.gameStatic = false;//

            this.startBtn = document.getElementById('start');
        },
        init: function () {
            this.config();//属性配置
            this.setArray();//布盘
            this.setBomb();//布雷
            this.setLeiCount();//计算每块周围雷数量
            this.bindEvent();//事件


            /*var url = window.location.href;
             var reg = /([^?=&]+)=([^?=&]+)/g;

             this.paramUrl = [];
             while (reg.test(url)) {
             this.paramUrl[RegExp.$1] = RegExp.$2;
             console.log(RegExp.$1 + '-' + RegExp.$2);
             }
             console.log(this.paramUrl);*/
        }
    }
    _obj.newObj();
})